import sys
import math
import typing
import random
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets


class Smoke(QGraphicsItem):
    def __init__(self, scene):
        super(Smoke, self).__init__()
        self.root = QFileInfo(__file__).absolutePath()

        self.pix = []
        for i in range(5):
            self.pix.append(QPixmap(self.root + "/res/smoke_0" + str(i + 1) + ".png"))

        for i in range(4):
            self.pix.append(QPixmap(self.root + "/res/robo_part_0" + str(i + 1) + ".png"))

        self.state = 0
        self.setVisible(False)
        self.random = random.random() * 0.1
        self.Times = 5000  # 记录绘制了多少次
        self.position = QPointF(0, 0)

        scene.addItem(self)

        self.Speed = 0

        self.Rotate = 0

        self.acceleration = 0.1

    def CallSmoke(self, StartX, StartY):  # 调出烟雾
        self.setRotation(0)
        self.position = QPointF(StartX, StartY)
        self.setPos(self.position)
        self.setVisible(True)
        self.Times = 0

    def CallSpareParts(self, StartX, StartY, Rotate):
        self.position = QPointF(StartX, StartY)
        self.setRotation(Rotate)
        self.setPos(self.position)
        self.setVisible(True)
        self.Times = 0

    def boundingRect(self):
        return QRectF(-self.pix[0].width() * 0.5, -self.pix[0].height() * 0.5, self.pix[0].width(),
                      self.pix[0].height())

    def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: typing.Optional[QWidget] = ...):

        painter.drawPixmap(-self.pix[self.state].width() * 0.5, -self.pix[self.state].height() * 0.5,
                           self.pix[self.state])
        # print(self.Times)

    def advance(self, phase: int):
        if self.isVisible and self.Times < 500:
            if self.state == 1:
                self.setRotation(0)
                self.setPos(self.mapToScene(self.random * 1, -self.random * 1))
                self.setRotation(self.Rotate)
            elif self.state == 2:
                self.setRotation(0)
                self.setPos(self.mapToScene(-self.random * 1, -self.random * 1))
                self.setRotation(self.Rotate)
            elif self.state == 3:
                self.setRotation(0)
                self.setPos(self.mapToScene(self.random * 1, -self.random * 2))
                self.setRotation(self.Rotate)
            elif self.state == 4:
                self.setRotation(0)
                self.setPos(self.mapToScene(-self.random * 1, -self.random * 2))
                self.setRotation(self.Rotate)
            elif self.state == 5:
                self.setPos(self.mapToScene(self.random * self.random * 2, self.random * 2))
            elif self.state == 6:
                self.setPos(self.mapToScene(self.random * self.random * 2, -self.random * 2))
            elif self.state == 7:
                self.setPos(self.mapToScene(self.random * self.random * 2, self.random * 1))
            else:
                self.setPos(self.mapToScene(self.random * self.random * 2, -self.random * 1))
            self.Times = self.Times + 1
            self.Rotate += self.acceleration
            self.acceleration += 0.1
        else:
            self.setVisible(False)
            self.Times = 0

    def setPos(self, pos: typing.Union[QPointF, QPoint]):

        super(Smoke, self).setPos(pos)

    def setRotation(self, angle: float):

        super(Smoke, self).setRotation(angle)
        '''
        设置角度时如果是烟雾则使用0角度，如果是零件则使用被射击的敌人的角度
        '''

    def setVisible(self, visible: bool):
        super(Smoke, self).setVisible(visible)

    def isVisible(self):
        return super(Smoke, self).isVisible()
